function updateTips( t ) {
                tips
                        .text( t )
                        .addClass( "ui-state-highlight" );
                setTimeout(function() {
                        tips.removeClass( "ui-state-highlight", 1500 );
                }, 500 );
        }

function checkLength( o, n, min, max ) {
        if ( o.val().length > max || o.val().length < min ) {
                o.addClass( "ui-state-error" );
                updateTips( "Length of " + n + " must be between " +
                        min + " and " + max + "." );
                return false;
        } else {
                return true;
        }
}

function checkRegexp( o, regexp, n ) {
        if ( !( regexp.test( o.val() ) ) ) {
                o.addClass( "ui-state-error" );
                updateTips( n );
                return false;
        } else {
                return true;
        }
}

function init_dialog_button(id, dialog_id) {
    
    $( '#'+id )
        .button()
        .click(function() {
            $( '#'+dialog_id ).dialog( "open" );
            return false;
        })
    
}

/*
 * Remove all the <select> option element
 */
function remove_all_options(element_id, empty_element) {
    
    $( '#'+element_id )
        .find('option')
        .remove()
        .end()
        .append('<option value="">'+empty_element+'</option>')
;
    
}

/*
 * Get the districts list 
 * 
 * @return string Districts list (JSON format)
 */
function get_districts_list(region_id, url, element_id) {
    
    $.ajax({
        url: url,
        type: 'POST',                 
        async: false,
        dataType: 'json',
        data: {region_id: region_id},
        success: function(districts_json) {
            //append di tutti gli elementi 
            $.each(districts_json, function(key, val) {
            $( '#'+element_id )
                .append($('<option></option>')
                            .attr('value', key)
                            .text(val)
                    );
        });
        }
    });
    
}

/*
 * Get the towns list 
 * 
 * @return string Towns list (JSON format)
 */
function get_towns_list(district_id, url, element_id) {

    $.ajax({
        url: url,
        type: 'POST',                 
        async: false,
        dataType: 'json',
        data: {district_id: district_id},
        success: function(towns_json) {
            //append di tutti gli elementi 
            $.each(towns_json, function(key, val) {
            $( '#'+element_id )
                .append($('<option></option>')
                            .attr('value', key)
                            .text(val)
                    );
        });
        }
    });

}